package com.ruoyi.web.controller.admin;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import com.ruoyi.system.domain.dto.HospitalSubscribeGroupPurchaseOrderDto;
import com.ruoyi.system.domain.vo.HospitalSubscribeGroupPurchaseOrderVo;
import com.ruoyi.system.report.MedicalReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.HospitalSubscribeGroupPurchaseOrder;
import com.ruoyi.system.service.IHospitalSubscribeGroupPurchaseOrderService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 团队订单Controller
 * 
 * @author ruoyi
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/admin/group/purchase/order")
public class HospitalSubscribeGroupPurchaseOrderAdminController extends BaseController
{
    @Autowired
    private IHospitalSubscribeGroupPurchaseOrderService hospitalSubscribeGroupPurchaseOrderService;

    @Autowired
    private MedicalReportService medicalReportService;

    /**
     * 查询团队订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HospitalSubscribeGroupPurchaseOrder hospitalSubscribeGroupPurchaseOrder)
    {
        startPage();
        List<HospitalSubscribeGroupPurchaseOrder> list = hospitalSubscribeGroupPurchaseOrderService.selectHospitalSubscribeGroupPurchaseOrderList(hospitalSubscribeGroupPurchaseOrder);
        return getDataTable(list);
    }

    /**
     * 新增团队订单
     */
    @PostMapping("/saveGroupPurchaseOrder")
    public AjaxResult save(@RequestBody HospitalSubscribeGroupPurchaseOrderDto dto)
    {
        return success(hospitalSubscribeGroupPurchaseOrderService.saveGroupPurchaseOrder(dto));
    }

    /**
     * 团队订单详情
     */
    @GetMapping("/getGroupPurchaseOrderInfo")
    public AjaxResult getGroupPurchaseOrderInfo(HospitalSubscribeGroupPurchaseOrder hospitalSubscribeGroupPurchaseOrder)
    {
        return success(hospitalSubscribeGroupPurchaseOrderService.getGroupPurchaseOrderInfo(hospitalSubscribeGroupPurchaseOrder));
    }

    /**
     * 保存团队订单详情 缓存
     */
    @PostMapping("/saveGroupPurchaseOrderInfoCache")
    public AjaxResult saveGroupPurchaseOrderInfo(@RequestBody HospitalSubscribeGroupPurchaseOrderVo vo)
    {
        return success(hospitalSubscribeGroupPurchaseOrderService.saveGroupPurchaseOrderInfo(vo));
    }

    /**
     * 查看团队订单详情 缓存
     */
    @GetMapping("/getGroupPurchaseOrderInfoCache/{id}")
    public AjaxResult getGroupPurchaseOrderInfoCache(@PathVariable("id")Long id)
    {
        return success(hospitalSubscribeGroupPurchaseOrderService.getGroupPurchaseOrderInfoCache(id));
    }

    /**
     * 提交团队订单
     */
    @PostMapping("/submitGroupPurchaseOrderInfo")
    public AjaxResult getGroupPurchaseOrderInfoCache(@RequestBody HospitalSubscribeGroupPurchaseOrderVo vo)
    {
        return success(hospitalSubscribeGroupPurchaseOrderService.submitGroupPurchaseOrderInfo(vo));
    }

    /**
     * 下载
     * @param hospitalSubscribeGroupPurchaseOrder
     * @return
     */
    @GetMapping(value = "/generate/image")
    public ResponseEntity<byte[]> generateReportImage(HospitalSubscribeGroupPurchaseOrder hospitalSubscribeGroupPurchaseOrder) {
        HospitalSubscribeGroupPurchaseOrderVo vo = hospitalSubscribeGroupPurchaseOrderService.getGroupPurchaseOrderInfo(hospitalSubscribeGroupPurchaseOrder);
        byte[] imageBytes = medicalReportService.generateMedicalReportImage(vo);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

        headers.setContentDispositionFormData("attachment", LocalDateTime.now().format(formatter) + ".png");
        return ResponseEntity.ok().headers(headers).body(imageBytes);
    }

}
